# -*- coding: utf-8 -*-
import json
import re
import requests
import scrapy
from scrapy.http import Request, FormRequest
from alimama.items import AlimamaItem
from urllib.parse import urlencode
import use_tkl_api


class TaobaoalmamaSpider(scrapy.Spider):
    searchkey = ''
    returnresult = ''
    items = ''
    name = 'taobaoalmama'
    cookie = {
     "t":"630b7ecd9ca65d46ec0e9fbbb05a8010",
     "account-path-guide-s1":"true",
     "107254468_yxjh-filter-1":"true",
     "undefined_yxjh-filter-1":"true",
     "cna":"7/yHEFcPlBgCAbSI4rlRkEw0",
     "Hm_lvt_407473d433e871de861cf818aa1405a1":"1513494099",
     "cookie2":"1af16bc3541443ceb0de7979df8405f8",
     "_tb_token_":"3388b17551753",
     "Hm_lvt_ba7c84ce230944c13900faeba642b2b4":"1524239288,1524286160,1524323135,1524365372",
     "v":"0",
     "Hm_lpvt_ba7c84ce230944c13900faeba642b2b4":"1524401171",
     "isg":"BGtrOerZtW5t5erp_gc7kMo3-4-VKH9lxbvii93oQKoBfIreZVAgUhwe0rwS3Nf6",
     "apush92cfbab62ba00f296071d06b58d6c782":"%7B%22ts%22%3A1524401190005%2C%22heir%22%3A1524401174917%2C%22parentId%22%3A1524369179097%7D",
     "alimamapwag":"TW96aWxsYS81LjAgKFdpbmRvd3MgTlQgNi4xOyBXaW42NDsgeDY0KSBBcHBsZVdlYktpdC81MzcuMzYgKEtIVE1MLCBsaWtlIEdlY2tvKSBDaHJvbWUvNjUuMC4zMzI1LjE4MSBTYWZhcmkvNTM3LjM2",
     "cookie32":"9ef9bc449cc420bda61c70b7d5c3c521",
     "alimamapw":"HwFZWA0WVxACCQlUbFRQVFUCB1MCBABUAVxXWl9VBldSA1JWV1MEUlZQUgAH",
     "cookie31":"MTA3MjU0NDY4LHllbGxvd2F1ZzAwMSw0NzU0MTE1NzdAcXEuY29tLFRC",
     "login":"W5iHLLyFOGW7aA%3D%3D"
    }

    def start_requests(self):  # 用start_requests()方法,代替start_urls

        # key = "https://detail.tmall.com/item.htm?id=566507586495"
        key = self.searchkey
        excelname = "1203"
        url = self.cheack_url(key)
        print('请求url：'+url)
        try:
            yield Request(url=url, cookies=self.cookie, callback=self.page, meta={'excelname': excelname})
        except TypeError:
            TaobaoalmamaSpider.items = '亲！请输入店铺链接或者淘口令'

    def page(self, response):
        # 解析返回查询结果
        # try:
            results = json.loads(response.body)
            resultlist = results["data"]["pageList"]
            for result in resultlist:
                # 月销biz30day
                yx = result['biz30day']
                # 店铺链接
                dplj = result['auctionUrl']
                # 关键字
                gjz = ''.join(result['title']).replace('<span class=H>', '').replace('</span>', '')
                # 优惠券couponAmount
                yhq = result['couponAmount']
                # 原价价格(券后价格)
                yj = result['zkPrice']
                qhjg = yj - yhq
                # 返现比例 tkCommonRate
                fxbl = result['tkRate']
                #是否有优惠券
                couponInfo=result['couponInfo']
                # auctionId
                auctionId = result['auctionId']
                # 立即推广
                urls = "http://pub.alimama.com/common/adzone/newSelfAdzone2.json?tag=29&itemId=" + str(auctionId)
                yield Request(url=urls, cookies=self.cookie, callback=self.next,
                              meta={'yx': yx, 'dplj': dplj,
                                    'gjz': gjz, 'qhjg': qhjg,
                                    'yhq': yhq, 'fxbl': fxbl,
                                    'auctionId': auctionId,'couponInfo': couponInfo, 'excelname': response.meta['excelname']})
        # except TypeError:
        #     print('该商品没有优惠券')
        #     TaobaoalmamaSpider.items = '亲！该商品没有优惠券'

    def next(self, response):
        # print(response.body)
        results = json.loads(response.body)
        otherAdzones = results["data"]["otherAdzones"]
        otherList = results["data"]["otherList"]
        adzoneid = ''
        siteid = ''
        for item, other in zip(otherAdzones, otherList):
            adzoneid = item['id']
            siteid = other['siteid']

        auctionid = response.meta['auctionId']
        urls = "http://pub.alimama.com/common/code/getAuctionCode.json?auctionid=" + str(
            auctionid) + "&adzoneid=" + str(adzoneid) + "&siteid=" + str(siteid) + "&scenes=1"
        print('获取淘口令url：' + urls)
        yield Request(url=urls, cookies=self.cookie, callback=self.getshortlink,
                      meta={'yx': response.meta['yx'], 'dplj': response.meta['dplj'],
                            'gjz': response.meta['gjz'], 'qhjg': response.meta['qhjg'],
                            'yhq': response.meta['yhq'], 'fxbl': response.meta['fxbl'],
                            'auctionId': auctionid, 'excelname': response.meta['excelname'],'couponInfo': response.meta['couponInfo']})

    def getshortlink(self, response):
            item = AlimamaItem()
            results = json.loads(response.body_as_unicode())
            print('传的券的价格：'+response.meta['couponInfo'])
            if response.meta['couponInfo'] == '无':  # 判断是否有优惠券
                item['短链接'] = results["data"]["shortLinkUrl"]
                item['淘口令'] = results["data"]["taoToken"]
                item['月销'] = response.meta['yx']
                # item['店铺链接'] = response.meta['dplj']
                item['关键字'] = response.meta['gjz']
                item['券后价格'] ='亲谢谢你的支持'
                # item['优惠券'] =''

                # item['excelname'] = response.meta['excelname']
                # item['返现比例'] = str(response.meta['fxbl']) + '%'
            else:
                item['短链接'] = results["data"]["couponShortLinkUrl"]
                item['淘口令'] = results["data"]["couponLinkTaoToken"]
                item['月销'] = response.meta['yx']
                # item['店铺链接'] = response.meta['dplj']
                item['关键字'] = response.meta['gjz']
                item['券后价格'] = response.meta['qhjg']
                item['优惠券'] = response.meta['yhq']

            TaobaoalmamaSpider.items = item
            yield item

    # 对输入框的处理
    def cheack_url(self, search_url):
        match_in_search = re.search('￥(.*?)￥', search_url, re.S)
        if match_in_search == None:
            match_url = re.search('htt.*(.*?)', search_url, re.S)
            if match_url != None:
                in_url = match_url.group(0)  # 返回长连接
                return self.url_address(in_url)
            else:
                key_result = search_url  # 返回关键词,改功能暂时停用因为会查出很多结果阿里妈妈会反爬需要重新登录甚至封号
                # return self.url_address(key_result)
        else:
            result_tkl = match_in_search.group(1)# 返回陶口令
            tkl_url = use_tkl_api.Tkl()
            try:
                tkl_url.tkl_web_api(result_tkl)
            except:
                tkl_url.aixi_web_api(result_tkl)
                print(tkl_url.api_count)
            url_re = tkl_url.taobao_url
            tkl_url.clear_data(url_text=url_re)
            re_url = tkl_url.result_url
            return self.url_address(re_url)
    # def tkl_api(self, result_tkl):
    #     taokouling = {
    #         'key': '',
    #         'tkl': result_tkl
    #     }
    #     api_url = 'http://api.w4.org.cn/api/detkl.php?' + urlencode(taokouling)  # 调用转换API
    #     # API申请网站：http://www.aixiwen.com/archives/44
    #     api_get = requests.get(api_url)
    #     api_result = json.loads(api_get.text)  # 陶口令转换成长链接
    #     in_url = api_result['url']
    #     print('淘口令之前的url：' + str(in_url))
    #     in_url_get = requests.get(in_url)
    #     print('淘口令处理之后的url' + str(in_url_get.url))
    #     return self.url_address(in_url_get.url)

    def url_address(self, in_url):
        aliurl_data = {
            'q': in_url,
            'toPage': '1',
            'perPageSize': '50',
            'shopTag': ''
        }
        mama_data = {
            'q': in_url,
            'toPage': '1',
            'yxjh': '-1'
        }
        alimama_url = 'http://pub.alimama.com/items/search.json?' + urlencode(aliurl_data)  # Ajax异步请求网址
        mama_url = 'http://pub.alimama.com/promo/search/index.htm?' + urlencode(mama_data)  # 静态页面网址
        print('\n', 'Ajax异步请求网址：' + alimama_url)
        return alimama_url

    def __str__(self):
        return (self.items)
